package com.archerdu.base.controller;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;

import javax.servlet.http.HttpServletRequest;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.ModelAndView;

import com.archerdu.commons.util.FileUtil;
import com.archerdu.commons.util.ImgUtil;

/**
 * 文件上传控制器
 * 
 * @projectName springDemo
 * @description 文件上传控制器
 * @author archer du
 * @createDate 2013年9月27日 下午5:29:28
 * @updateUser dxf
 * @updateDate 2013年9月27日 下午5:29:28
 * @updateRemark 新建
 * @version 0.0.1
 */
@Controller
@RequestMapping(value = "/common")
public class FileUploadController extends BaseController {

    private static final long serialVersionUID = 4763069862840822079L;

    @RequestMapping(value = "/upload", method = RequestMethod.GET)
    public ModelAndView uploadPath(@RequestParam("savePath") String savePath) {
        ModelAndView mav = new ModelAndView();
        mav.addObject("savePath", savePath);
        mav.setViewName("common/upload");
        return mav;
    }

    @RequestMapping(value = "/upload", method = RequestMethod.POST)
    public ModelAndView handleFormUpload(HttpServletRequest requ, @RequestParam("returnUrl") String returnUrl,
            @RequestParam("compress") boolean compress, Integer width, Integer height,
            @RequestParam("newname") boolean newname, @RequestParam("savePath") String savePath,
            @RequestParam("file") MultipartFile file) {

        ModelAndView mav = new ModelAndView();

        // 自动创建路径
        File path = new File(requ.getSession().getServletContext().getRealPath("/") + "/upload/" + savePath);
        logger.debug("upload path " + path.getAbsolutePath() + " is " + path.exists());

        if (!path.exists()) {
            path.mkdirs();
            logger.info("make update path " + path.getAbsolutePath());
        }

        // 文件名是否重新命名
        String uploadFileName = file.getOriginalFilename();
        if (newname) {
            uploadFileName = Long.toString(System.currentTimeMillis()) + "." + FileUtil.getSuffix(uploadFileName);
        }

        try {
            logger.debug("file.getOriginalFilename() is " + file.getOriginalFilename());
            // 是否压缩文件
            if (compress) {
                // 确定压缩比例
                if (width == null || height == null) {
                    width = 400;
                    height = 400;
                }
                ImgUtil.compress(file.getInputStream(), new FileOutputStream(path.getAbsolutePath() + "/"
                        + uploadFileName), width, height, true, false);
            } else {
                FileUtil.copyByByte(file.getInputStream(), new FileOutputStream(path.getAbsolutePath() + "/"
                        + uploadFileName), 1024);
            }
        } catch (IOException e) {
            logger.error(e.getMessage(), e);
        }
        mav.addObject("uploadFileName", "/upload/" + savePath + uploadFileName);
        mav.setViewName(returnUrl);
        return mav;
    }
}
